package com.kinon.gfcanteen_v1.view.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.kinon.gfcanteen_v1.R;
import com.kinon.gfcanteen_v1.view.activity.OrderDetailActivity;
import com.kinon.gfcanteen_v1.view.base.BaseFragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

/**
 * Created by lhqq on 2018-01-08.
 */

public class TakemealFragment extends BaseFragment implements View.OnClickListener {

    private View mView;
    private TextView tv_title;
    private Button btn_scanit;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView=inflater.inflate(R.layout.fragment_takemeal_layout,container,false);
        return mView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
    }

    private void initView() {
        tv_title=mView.findViewById(R.id.tv_title);
        btn_scanit=mView.findViewById(R.id.btn_scanit);
        tv_title.setText("取餐");
        addListener();
    }
    private void addListener(){
        btn_scanit.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        Intent intent=new Intent("com.summi.scan");
        intent.setPackage("com.sunmi.sunmiqrcodescanner");
        startActivityForResult(intent,2);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.e(TAG, "requestCode "+requestCode );
        Log.e(TAG, "resultCode "+resultCode );
        if(requestCode==2 && data!=null){
            Bundle bundle=data.getExtras();
            ArrayList<HashMap<String, String>> result =
                    (ArrayList<HashMap<String, String>>) bundle.getSerializable("data");
            Iterator<HashMap<String, String>> it = result.iterator();
            String cardNo="";
            while (it.hasNext()){
                HashMap<String, String> hashMap = it.next();
                cardNo=hashMap.get("VALUE");
            }
            if(!TextUtils.isEmpty(cardNo)){
                Intent intent=new Intent(getActivity(), OrderDetailActivity.class);
               intent.putExtra("orderon",cardNo);
                startActivity(intent);
            }


            Log.e(TAG, "cardNo== "+cardNo);

        }

    }
}
